
local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local HomeGui=UIManager.ui_list.HomeGui;
local OpEggGui=UIManager:CreateUI("OpEggGui",require(script.OpEggGui))

local Tool=require(script.Parent.tool)
local gCfg=Tool.GetConfg()

local LanguageHelper=_G.LanguageHelper;
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local FunctionList = ClientOHOH.load.FunctionList();
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=self.parent

    self.needTime=gCfg.OnlineTime;

    self.dataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
    self.activityData = self.dataBase:GetData("activity_1");


    CallBackManager:AddRbxConnect(self,HomeGui.insts.OpEgg.button.Activated, function()
        -- print(" OpEggGui:Show();")
        OpEggGui:Show();
	end);


    CallBackManager:AddRbxConnect(self,OpEggGui.FreeEvent, function()

       if  self.activityData.count>0 and self:Invoke(FunctionList.open_opegg) then
        else
            GlobalTipGui:Show(LanguageHelper:GetText(530014));
       end
	end);

    CallBackManager:AddRbxConnect(self,OpEggGui.BuyEvent, function()
        self.player.RobuxBuyEvent:Action(gCfg.GoodsId)
     end);
     
    self.parent.UserDataEvent:Connect(function(data)
        if data["activity_1"]then
            self.activityData = self.dataBase:GetData("activity_1");
            OpEggGui:FreeCount(data["activity_1"].count);
            HomeGui:SetOPEggRed(data["activity_1"].count)
        end
    end);

    HomeGui:SetOPEggRed(self.activityData.count)
    OpEggGui:FreeCount(self.activityData.count);

   local eggCfg=_G.CommonOHOH.config.Egg[gCfg.EggId]

    local min=math.floor(gCfg.OnlineTime/60)
    local name=_G.LanguageHelper:GetText(eggCfg.name)
    local petIdList={}

    for _,v in pairs(eggCfg.petIds) do
        local petId=_G.CommonOHOH.load.PetTool().GetPetId(v)
        table.insert(petIdList,petId)
    end
    OpEggGui:Init(min,name,gCfg.Price,petIdList,self.dataBase)

    self:TaskDo();
end;


function  M.TaskDo(self)
	if  self.task then
		return;
	end

	self.task=_G.CommonOHOH.load.OnLineTask().new(self.parent,self.needTime,"activity_1");

    self.taskUpdateConnect=self.task.UpdateEvent:Connect(function()
        local now,flish= self.task:GetProgress();
      
        OpEggGui:UpdateProgress(now/flish)
        local timeStr=GeneralTool.secondsToHHMM(flish-now);
        OpEggGui:UpdateTime(timeStr)
        HomeGui.insts.OpEgg.CountDownTime.Text=timeStr;
	   
    end)

	self.taskCompleteConnect=self.task.CompleteEvent:Connect(function()
		if self.taskCompleteConnect then
			self.taskCompleteConnect:Disconnect();
			self.taskCompleteConnect=nil;
		end

        if self.taskUpdateConnect then
			self.taskUpdateConnect:Disconnect();
			self.taskUpdateConnect=nil;
		end
        OpEggGui:ClearTask();
		self.task=nil;
		self:TaskDo();
	end)

end


function M.dtor(self)
    
    if self.taskCompleteConnect  then
		self.taskCompleteConnect:Disconnect();
		self.taskCompleteConnect=nil;
	end
    if self.taskUpdateConnect then
        self.taskUpdateConnect:Disconnect();
        self.taskUpdateConnect=nil;
    end
	if self.task then
		self.task:dtor();
    end
	M.super.dtor(self);
end;
return M;